package com.loe.ktui.custom

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.core.content.res.getBooleanOrThrow
import androidx.core.content.res.getColorOrThrow
import androidx.core.content.res.getDimensionOrThrow
import androidx.core.content.res.getResourceIdOrThrow
import com.loe.ktui.*
import com.loe.ktui.ktPx
import com.loe.ktui.ktSafe
import com.loe.ktui.util.KtColor
import com.loe.ktui.util.KtCornerProvider

open class UiCheckText(id: String? = null) : UiText(id)
{
    var checkImage0: Any? = null
    var checkImage1: Any? = null
    var checkImage2: Any? = null

    var checkColor0: Int? = null
    var checkColor1: Int? = null
    var checkColor2: Int? = null

    override var imageTop: Any? = 1
    override var imageWidth: Any? = 15
    override var imageHeight: Any? = 15
    override var imagePadding: Any? = 6

    var isEnable: Boolean? = null
    var isChecked: Boolean = false

    var onCheck: (KtCheckText.(isCheck: Boolean) -> Unit)? = null

    open fun initUI(context: Context, view: KtCheckText)
    {
        super.initUI(context, view)

        checkImage0?.let { view.checkImage0 = it }
        checkImage1?.let { view.checkImage1 = it }
        checkImage2?.let { view.checkImage2 = it }
        checkColor0?.let { view.checkColor0 = it }
        checkColor1?.let { view.checkColor1 = it }
        checkColor2?.let { view.checkColor2 = it }

        imageTop?.let { view.imageTop = it.dist }
        imageWidth?.let { view.imageWidth = it.dist }
        imageHeight?.let { view.imageHeight = it.dist }
        imagePadding?.let { view.imagePadding = it.dist }

        view.onCheck = null
        isEnable?.let { view.isEnable = it }
        view.isChecked = isChecked
        onCheck?.let { onCheck -> view.setOnCheck { view.onCheck(it) } }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtCheckText
    {
        return KtCheckText(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_CheckText(id: String? = null, block: (UiCheckText.() -> Unit)? = null): UiCheckText
{
    val ui = UiCheckText(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

open class KtCheckText : KtTextView
{
    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtCheckText))
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtCheckText, defStyleAttr, 0))
    }

    private fun initXML(typedArray: TypedArray? = null)
    {
        setOnClickListener()
        {
            ktSafe()
            {
                if (isEnable)
                {
                    toggle()
                }
            }
        }
        typedArray?.let()
        {
            if (it.hasValue(R.styleable.KtCheckText_kt_corner))
            {
                val corner = it.getDimensionOrThrow(R.styleable.KtCheckText_kt_corner).toInt()
                val cornerDirect =  KtDirects[it.getInt(R.styleable.KtCheckText_kt_cornerDirect, 4)]

                outlineProvider = KtCornerProvider(corner, cornerDirect, this)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_isAlphaClick))
            {
                isAlphaClick = it.getBooleanOrThrow(R.styleable.KtCheckText_kt_isAlphaClick)
            }

            if (it.hasValue(R.styleable.KtCheckText_kt_check_image0))
            {
                checkImage0 = it.getResourceIdOrThrow(R.styleable.KtCheckText_kt_check_image0)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_check_image1))
            {
                checkImage1 = it.getResourceIdOrThrow(R.styleable.KtCheckText_kt_check_image1)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_check_image2))
            {
                checkImage2 = it.getResourceIdOrThrow(R.styleable.KtCheckText_kt_check_image2)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_check_color0))
            {
                checkColor0 = it.getColorOrThrow(R.styleable.KtCheckText_kt_check_color0)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_check_color1))
            {
                checkColor1 = it.getColorOrThrow(R.styleable.KtCheckText_kt_check_color1)
            }
            if (it.hasValue(R.styleable.KtCheckText_kt_check_color2))
            {
                checkColor2 = it.getColorOrThrow(R.styleable.KtCheckText_kt_check_color2)
            }

            imageTop = it.getDimensionPixelOffset(R.styleable.KtCheckText_kt_imageTop, ktPx(1.0))
            imageWidth = it.getDimensionPixelOffset(R.styleable.KtCheckText_kt_imageWidth, ktPx(15.0))
            imageHeight = it.getDimensionPixelOffset(R.styleable.KtCheckText_kt_imageHeight, ktPx(15.0))
            imagePadding = it.getDimensionPixelOffset(R.styleable.KtCheckText_kt_imagePadding, ktPx(6.0))

            if (it.hasValue(R.styleable.KtCheckText_kt_isEnable))
            {
                isEnable = it.getBooleanOrThrow(R.styleable.KtCheckText_kt_isEnable)
            }

            isChecked = it.getBoolean(R.styleable.KtCheckText_kt_isChecked, false)

            it.recycle()
        }
    }

    /********************************************************************************************/

    internal var checkImage0: Any = "kt_icon_check_0"
    internal var checkImage1: Any = "kt_icon_check_1"
    internal var checkImage2: Any = "kt_icon_check_2"

    internal var checkColor0 = KtColor.textLight
    internal var checkColor1 = KtColor.main
    internal var checkColor2 = KtColor.gray

    fun toggle()
    {
        isChecked = !isChecked
    }

    fun notifyCheck()
    {
        if (isEnable)
        {
            if (isChecked)
            {
                setNowImage(checkImage1)
                imageColor = checkColor1
            } else
            {

                setNowImage(checkImage0)
                imageColor = checkColor0
            }
        } else
        {
            if (isChecked)
            {
                setNowImage(checkImage2)
                imageColor = checkColor2
            } else
            {

                setNowImage(checkImage0)
                imageColor = checkColor0
            }
        }
        notifyImage()
    }

    private fun setNowImage(img: Any?)
    {
        if (img is Int)
        {
            imageRes = img
            image = null
        } else
        {
            image = img
            imageRes = null
        }
    }

    var isEnable = true
        set(value)
        {
            field = value
            notifyCheck()
        }

    var isChecked = false
        set(value)
        {
            field = value
            notifyCheck()
            onCheck?.invoke(isChecked)
        }

    fun setCheckedNoOn(isChecked: Boolean)
    {
        val on = onCheck
        onCheck = null
        this.isChecked = isChecked
        onCheck = on
    }

    internal var onCheck: ((isCheck: Boolean) -> Unit)? = null

    fun setOnCheck(onCheck: (isCheck: Boolean) -> Unit)
    {
        this.onCheck = onCheck
    }
}